package com.example.mygame.utils;

import com.example.mygame.model.BackupTask;
import com.example.mygame.model.EquipmentInfo;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;


public class ReadCsvUtils {
    private static String path = "E:\\date\\file\\myGame";

    /**
     *
     * @param name
     * @return
     */
    public static List<EquipmentInfo> read(String name) {
        List<EquipmentInfo> result = new ArrayList<>();
        try (Reader reader = Files.newBufferedReader(Paths.get(path +  "\\"+name))) {
            CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(',');
            Iterable<CSVRecord> records = format.parse(reader);
            for (CSVRecord record : records) {
                EquipmentInfo info = new EquipmentInfo();
                // 按顺序取值
                info.setId(record.get(0));
                info.setName(record.get(1));
                info.setDesc(record.get(2));
                result.add(info);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return result;
        }
        return result;
    }
    /**
     *
     * @param name
     * @return
     */
    public static List<BackupTask> read2(String name) {
        List<BackupTask> result = new ArrayList<>();
        try (Reader reader = Files.newBufferedReader(Paths.get(path +  "\\"+name))) {
            CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(',');
            Iterable<CSVRecord> records = format.parse(reader);
            for (CSVRecord record : records) {
                BackupTask info = new BackupTask();
                // 按顺序取值
                info.setName(record.get(0));
                info.setType(record.get(1));
                info.setStartTime(record.get(2));
                info.setEndTime(record.get(3));
                info.setIsUpdate(Integer.parseInt(record.get(4)));
                info.setStatus(Integer.parseInt(record.get(5)));
                result.add(info);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return result;
        }
        return result;
    }

    public static boolean  save2(List<BackupTask> list, String name) throws IOException {
        String csvFilePath = path +  "\\"+name;
        // 定义 CSV 表头
        String[] headers = {"name", "startTime", "endTime", "isUpdate", "status"};
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(csvFilePath, StandardCharsets.UTF_8));
             CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(headers).withRecordSeparator("\n"))) {
            for (BackupTask equipment : list) {
                // 逐个提取字段值，确保按表头顺序排列,否则一行的多个字段会写到一个位置
                csvPrinter.printRecord(
                        equipment.getName(),    // 提取id字段
                        equipment.getType(),  // 提取name字段
                        equipment.getStartTime(),
                        equipment.getEndTime(),
                        equipment.getIsUpdate(),
                        equipment.getStatus()// 提取desc字段
                );
            }
            return true;
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public static boolean  save(List<EquipmentInfo> list, String name) throws IOException {
        String csvFilePath = path +  "\\"+name;
        // 定义 CSV 表头
        String[] headers = {"id", "name", "desc"};
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(csvFilePath, StandardCharsets.UTF_8));
             CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(headers).withRecordSeparator("\n"))) {
            for (EquipmentInfo equipment : list) {
                // 逐个提取字段值，确保按表头顺序排列,否则一行的多个字段会写到一个位置
                csvPrinter.printRecord(
                        equipment.getId(),    // 提取id字段
                        equipment.getName(),  // 提取name字段
                        equipment.getDesc()   // 提取desc字段
                );
            }
            return true;
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    }